package client.comm.baoding.dialog

import android.view.View
import android.widget.LinearLayout
import androidx.databinding.Observable
import androidx.databinding.ObservableField
import client.comm.commlib.base.BaseActivity
import client.comm.baoding.R
import client.comm.baoding.databinding.DialogSelectskuBinding
import client.comm.baoding.utils.NavigationBar1.getBottomNavigatorHeight
import client.comm.baoding.utils.StatusBarUtil4
import client.comm.commlib.dipToPx
import client.tzstore.oo.dialog.BottomBindingDialog
import kotlin.properties.Delegates

class SelectSkuDialog(val context: BaseActivity<*, *>, val type: String) :
    BottomBindingDialog<DialogSelectskuBinding>(context) {
    val activity: BaseActivity<*, *>

    lateinit var id: String
    var price by Delegates.notNull<Double>()

    //    var lpq_price by Delegates.notNull<Double>()
//    var integral by Delegates.notNull<Double>()
    lateinit var imgUrl: String
    lateinit var name: String

    val selectNum = ObservableField<String>("1").apply {
        addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
            override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
                sender?.let {
                    it as ObservableField<String>
                    var num = 0
                    try {
                        if (!it.get().isNullOrEmpty())
                            num = it.get()!!.toInt()
                    } catch (e: Exception) {
                    }

                    binding.reducueTv.isEnabled = num > 1
                }
            }
        })
    }

    init {
        activity = context
    }

    override fun getV(): Int {
        return R.layout.dialog_selectsku
    }

    override fun postBinding() {
        binding.event = this
        window?.setWindowAnimations(R.style.dialogWindowAnimButtomToTop);
        window?.setBackgroundDrawableResource(android.R.color.transparent)

        activity.getImgLoader().load(imgUrl).into(binding.img)
        if (type.isNotEmpty() && type.equals("1")) {

            var layout = binding.btnOk.layoutParams as LinearLayout.LayoutParams
            if (!StatusBarUtil4.isNavBarHide(context)) {
                layout.bottomMargin = getBottomNavigatorHeight(context) + dipToPx(context.resources!!, 20.0f).toInt()
            } else {
                layout.bottomMargin = 0 //将默认的距离底部改为0
            }
            binding.btnOk.layoutParams = layout
//            binding.flDialogSku.setPadding(0, 0, 0, dipToPx(context.resources!!, 50.0f).toInt())
        } else {

        }

        binding.commPriceIntegral.apply {
            var is_price_show: Boolean = false
            var is_integral_unit_show: Boolean = false

            if (price > 0) {
                tvPrice.text = "${price}"
                is_price_show = true
//                if (integral > 0) {
//                    tvIntegral.text = "+" + integral //+ "积分"
//                    is_integral_unit_show = true
//                } else {
//                    tvIntegral.text = ""
//                    is_integral_unit_show = false
//                }

            } /*else if (lpq_price > 0) {
                TextTool.getBuilder("${lpq_price}")
                    .setAlign(Layout.Alignment.ALIGN_CENTER).setTextSize(18)
                    .append("体验券").setForegroundColor(Color.parseColor("#E81837"))
                    .setTextSize(12)
                    .into(tvPrice)
                is_price_show = true
                if (integral > 0) {
                    tvIntegral.text = "+" + integral //+ "积分"
                    is_integral_unit_show = true
                } else {
                    tvIntegral.text = ""
                    is_integral_unit_show = false
                }

            }*/ else {
                tvPrice.text = ""
                is_price_show = false
//                if (integral > 0) {
//                    tvIntegral.text = "" + integral //+ "积分"
//                    is_integral_unit_show = true
//                } else {
//                    tvIntegral.text = ""
//                    is_integral_unit_show = false
//                }
            }

            if (is_price_show) {
                tvPriceUnit.visibility = View.VISIBLE
            } else {
                tvPriceUnit.visibility = View.GONE
            }

            if (is_integral_unit_show) {
                tvIntegralDesc.visibility = View.VISIBLE
            } else {
                tvIntegralDesc.visibility = View.GONE
            }
        }

//        if (price > 0) {
//            binding.tvPrice.text = "¥${price}"
//            if (integral > 0) {
//                binding.tvIntegral.text = "+" + integral + "积分"
//            } else {
//                binding.tvIntegral.text = ""
//            }
//
//        } else {
//            binding.tvPrice.text = ""
//            if (integral > 0) {
//                binding.tvIntegral.text  = "" + integral + "积分"
//            } else {
//                binding.tvIntegral.text = ""
//            }
//
//        }
    }

    fun commPrice(price: Double, integral: Double) {

    }


    fun changeNum(n: Int) {
        var num = 0
        try {
            num = selectNum.get()!!.toInt()
        } catch (e: Exception) {
        }
        num += n
        if (num < 1)
            num = 1
        selectNum.set("$num")
    }

    var ok: ((uri: String) -> Unit)? = null

    fun queren() {
        cancel()
        ok?.invoke(selectNum.get() as String)
    }
}